#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>


  struct ListNode
  {
     int val;
     struct ListNode *next;
  };
 
struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB) {
    int i = 0;
    int j = 0;
    struct ListNode* cur1 = headA;
    struct ListNode* cur2 = headB;
    while (cur1 && cur1->next)
    {
        i++;
        cur1 = cur1->next;
    }
    while (cur2 && cur2->next)
    {
        j++;
        cur2 = cur2->next;
    }
    if (cur1 != cur2)
    {
        return NULL;
    }
    int k = abs(i - j);
    if (i > j)
    {
        while (k--)
        {
            headA = headA->next;
        }
    }
    else
    {
        while (k--)
        {
            headB = headB->next;
        }
    }
    while (headB != headA)
    {
        headB = headB->next;
        headA = headA->next;
    }
    return headB;
}